package core.cards.election.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import core.cards.election.model.VotingCard;
import core.gui.ValidationWizardPage;

public class WzdPgAddEditVotingCard extends ValidationWizardPage {

	private boolean addVotingCard;
	private VotingCard votingCard;

	private Spinner spnnrChecksAllowed;
	private Text txtDescription;
	private Text txtName;

	/**
	 * Create the wizard.
	 * 
	 * @param votingCard
	 * @param addVotingCard
	 */
	public WzdPgAddEditVotingCard(boolean addVotingCard, VotingCard votingCard) {
		super("WzdPgAddEditVotingCard");

		if (addVotingCard) {
			setTitle("Add Voting Card");
			setDescription("Add new Voting Card to the Election.");
		} else {
			setTitle("Edit Voting Card");
			setDescription("Edit an existing Voting Card of the Election.");
		}

		this.addVotingCard = addVotingCard;
		this.votingCard = votingCard;
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite cmpMain = new Composite(parent, SWT.NULL);

		setControl(cmpMain);

		cmpMain.setLayout(new GridLayout(2, false));

		new Label(cmpMain, SWT.NONE).setText("Name:");

		txtName = new Text(cmpMain, SWT.BORDER | SWT.SINGLE);
		txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		Label lblDescription = new Label(cmpMain, SWT.NONE);
		lblDescription.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		lblDescription.setText("Description:");

		txtDescription = new Text(cmpMain, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		GridData gd_descriptionText = new GridData(SWT.FILL, SWT.TOP, true, false);
		gd_descriptionText.heightHint = 95;
		txtDescription.setLayoutData(gd_descriptionText);

		new Label(cmpMain, SWT.NONE).setText("ChecksAllowed:");

		spnnrChecksAllowed = new Spinner(cmpMain, SWT.BORDER);
		spnnrChecksAllowed.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		initValues();
		setValidationListeners();
	}

	private void initValues() {
		if (addVotingCard) {
			setPageComplete(false);
		} else {
			txtName.setText(votingCard.getName());
			txtDescription.setText(votingCard.getDescription());
			spnnrChecksAllowed.setSelection(votingCard.getChecksAllowed());
		}
	}

	private void setValidationListeners() {
		txtName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
		txtDescription.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
		spnnrChecksAllowed.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});
	}

	@Override
	protected void validate() {
		String name = txtName.getText().trim();
		String description = txtDescription.getText().trim();
		Integer checksAllowed = new Integer(spnnrChecksAllowed.getText().trim());

		// error
		if (name.length() == 0) {
			setErrorState("Name cannot be empty.");
			return;
		}
		if (description.length() == 0) {
			setErrorState("Description cannot be empty.");
			return;
		}
		if (checksAllowed == 0) {
			setErrorState("Checks allowed must be more than 0.");
			return;
		}

		// ok
		clearErrorState();
	}

	@Override
	protected void update() {
		votingCard.setName(txtName.getText().trim());
		votingCard.setDescription(txtDescription.getText().trim());
		votingCard.setChecksAllowed(new Integer(spnnrChecksAllowed.getText().trim()));
	}
}
